/**
 * @Author:Geln Yang
 * @Created:2008-10-1 $ Revision: 1.0 $ $ Author: Geln Yang $ $ Date: 2008-10-1 $
 */
package com.hgy.commonsale.webui.action.user;

import org.apache.commons.lang.StringUtils;
import org.geln.encode.EncodeUtil;

import com.hgy.commonsale.user.domain.User;
import com.hgy.commonsale.webui.action.ProtectedAction;

/**
 * @Author:Geln Yang
 * @Created:2008-10-1
 */
public class ChangePasswordAction extends ProtectedAction {
	private String password;
	private String oldPassword;

	@Override
	public String execute() throws Exception {
		if (StringUtils.isEmpty(password) || StringUtils.isEmpty(oldPassword)) {
			addActionError("user.password.required");
			return ERROR;
		}
		oldPassword = EncodeUtil.hash(oldPassword);

		User user = sessionContext.getUser();
		if (!user.getPassword().equals(oldPassword)) {
			addActionError("user.oldPassword.wrong");
			return ERROR;
		}
		user.setPassword(password);
		userManager.saveOrUpdate(user);
		return SUCCESS;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
}
